package com.csd.action.manager;

import java.util.List;
import java.util.Random;

import com.csd.exception.GetException;
import com.csd.helper.ActionResult;
import com.csd.helper.ProjectState;
import com.csd.pojo.EvaluateItem;
import com.csd.pojo.Project;
import com.csd.pojo.Teacher;
import com.csd.service.IEvaluateItemService;
import com.csd.service.IProjectService;
import com.csd.service.IProjectTypeService;
import com.csd.service.ITeacherService;
import com.csd.vo.EvaluateItemVO;

/**
 * 进入评审页面，查出评审老师和可选评审老师
 * @author sanmu
 *	<br>创建于2009-12-29
 */
public class EvaluationTeacherListAction {
	
	private Integer projectID;
	
	private String stage;
	
	private Integer teacherID;
	
	private Integer evaluateItemID;
	
	
	
	private List<EvaluateItemVO> evaluationTeacherList;
	
	private IEvaluateItemService evaluateItemService;
	
	private ITeacherService teacherService;
	
	private IProjectTypeService projectTypeService;
	
	private IProjectService projectService;
	
    
	
	
	public IProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	public IProjectTypeService getProjectTypeService() {
		return projectTypeService;
	}

	public void setProjectTypeService(IProjectTypeService projectTypeService) {
		this.projectTypeService = projectTypeService;
	}

	public Integer getEvaluateItemID() 
	{
		return evaluateItemID;
	}

	public void setEvaluateItemID(Integer evaluateItemID) 
	{
		this.evaluateItemID = evaluateItemID;
	}

	public Integer getTeacherID() {
		return teacherID;
	}

	public void setTeacherID(Integer teacherID) {
		this.teacherID = teacherID;
	}

	public String getStage() {
		return stage;
	}

	public void setStage(String stage) {
		this.stage = stage;
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public Integer getProjectID() {
		return projectID;
	}

	public void setProjectID(Integer projectID) {
		this.projectID = projectID;
	}


	public List<EvaluateItemVO> getEvaluationTeacherList() {
		return evaluationTeacherList;
	}

	public void setEvaluationTeacherList(List<EvaluateItemVO> evaluationTeacherList) {
		this.evaluationTeacherList = evaluationTeacherList;
	}

	public IEvaluateItemService getEvaluateItemService() 
	{
		return evaluateItemService;
	}

	public void setEvaluateItemService(IEvaluateItemService evaluateItemService) 
	{
		this.evaluateItemService = evaluateItemService;
	}
	
	@SuppressWarnings("unused")
	private void getEvaluateItem(Integer projectID, Integer teacherID)
	{
		setEvaluateItemID(evaluateItemService.getEvaluateItemID(projectID, teacherID));
	}
	
	public String execute()throws Exception
	{
		String text = ActionResult.fail;
		String state = "";
		int max = 0;
		
		boolean bl = false;
		EvaluateItem eItem = new EvaluateItem();
		Project project = new Project();
		
		if(stage.equals("1"))
		{
			state = ProjectState.beforeStage;
		}
		else if(stage.equals("2"))
		{
			state = ProjectState.middleStage;
		}
		else if(stage.equals("3"))
		{
			state = ProjectState.afterStage;
		}
		else
		{
			return text;
		}
		eItem.setStage(state);
		
		//查出评审老师和可选评审老师
		evaluationTeacherList = evaluateItemService.getEvaluationTeacher(projectID, state);
		max = projectTypeService.getProjectTypeMaxTeacherByID(projectService.getProjectByID(projectID).getProjectType().getId());
		
			try
			{
				if(evaluationTeacherList.size() < max)
				{
					List<Teacher> teachers = teacherService.findTeachersExceptInstructor(projectID);
					Random rand = new Random();
					
					for(int i = evaluationTeacherList.size(); i < max;)
					{
						int count = rand.nextInt(teachers.size());
						Teacher evaluateTeacher = teachers.get(count);
						EvaluateItemVO evo = new EvaluateItemVO();
						
						if(evaluationTeacherList.size() == 0)
						{
							evo.setTeacherID(evaluateTeacher.getId());
							evo.setTeacherName(evaluateTeacher.getName());
							
							i++;
							project.setId(projectID);
							eItem.setProject(project);
							eItem.setTeacher(evaluateTeacher);
							eItem.setGrade(0);
							//保存评审项

							bl = evaluateItemService.saveEvaluateItem(eItem);
							
							if (!bl) 
							{
								text = ActionResult.fail;
							}
							else
							{
								evaluationTeacherList.add(evo);
							}
							
						}
						else if(evaluationTeacherList.size() >= 1)
						{
							int countLoop = 0;
							for(countLoop = 0; countLoop < evaluationTeacherList.size(); countLoop++)
							{
								if(evaluationTeacherList.get(countLoop).getTeacherID().equals(evaluateTeacher.getId()))
								{
									break;
								}
							}
							
							if(countLoop == evaluationTeacherList.size())
							{
								evo.setTeacherID(evaluateTeacher.getId());
								evo.setTeacherName(evaluateTeacher.getName());
								
								i++;
								project.setId(projectID);
								eItem.setProject(project);
								eItem.setTeacher(evaluateTeacher);
								eItem.setGrade(0);
								//保存评审项
								bl = evaluateItemService.saveEvaluateItem(eItem);
								
								if (!bl) 
								{
									text = ActionResult.fail;
								}
								else
								{
									evaluationTeacherList.add(evo);
								}
							}
						}
						getEvaluateItem(projectID, evaluateTeacher.getId());
					}
				}
			}
			catch (Exception e)
			{
				throw new GetException(e, "查找出错！");
			}
			
		
		text = ActionResult.success;
		return text;
	}
}
